package com.example.android.story.ui;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProvider;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android.story.R;
import com.example.android.story.Utils.Database;
import com.example.android.story.Utils.LocalDatabase;
import com.example.android.story.Utils.RememberMe;
import com.example.android.story.Utils.StoryViewModel;
import com.example.android.story.Utils.User;

import java.util.List;

import cn.bmob.v3.Bmob;

public class LoginActivity extends AppCompatActivity implements SignupFragment.OnClick {

    private FragmentManager fragmentManager;
    private SignupFragment signupFragment;
    private StoryViewModel viewModel;
    LiveData<User> rememberMe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        viewModel= ViewModelProviders.of(this).get(StoryViewModel.class);
        viewModel.getAllUsers().observe(this, new Observer<List<User>>() {
            @Override
            public void onChanged(@Nullable List<User> users) {
                Log.d("debug","users loaded in LoginActivity, users size :"+Integer.toString(users.size()));
            }
        });

        final EditText usernameEdit=((EditText)findViewById(R.id.username));
        final EditText password=((EditText)findViewById(R.id.password));

        rememberMe=viewModel.getRememberMe();
        rememberMe.observe(this, new Observer<User>() {
            @Override
            public void onChanged(@Nullable User user) {
                CheckBox checkBox=findViewById(R.id.remeber);
                if(user!=null){
                    usernameEdit.setText(user.getName());
                    password.setText(user.getPassword());
                    checkBox.setChecked(true);
                }
                else
                {
                    checkBox.setChecked(false);
                }

            }
        });

        fragmentManager=getSupportFragmentManager();
        signupFragment = (SignupFragment) fragmentManager.findFragmentById(R.id.signin_fragment);
        fragmentManager.beginTransaction().hide(signupFragment).commit();


        TextView signin= (TextView) findViewById(R.id.sign_in);
        signin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                fragmentManager.beginTransaction().show(signupFragment).commit();
            }
        });

        Button login=(Button)findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                User user=null;
                if(rememberMe.getValue()!=null)
                {
                    user=viewModel.getUser(rememberMe.getValue().getName());
                }
                else{
                    String username=((EditText)findViewById(R.id.username)).getText().toString();
                    String password=((EditText)findViewById(R.id.password)).getText().toString();
                    user=viewModel.getUser(username);
                    if(user==null){
                        promtMsg("用户名"+username+"不存在");
                        return;
                    }
                    else if(!user.comparePassword(password)){
                        promtMsg("密码不正确");
                        return;
                    }
                }
                CheckBox checkBox=findViewById(R.id.remeber);
                if(checkBox.isChecked()){
                    RememberMe rememberMe=new RememberMe();
                    rememberMe.setName(user.getName());
                    viewModel.insert(rememberMe);
                }
                else{
                    viewModel.eraseRememberMe();
                }

                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                intent.putExtra("username",user.getName());
                startActivity(intent);
            }
        });
    }

    public void promtMsg(String s) {
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
        Log.d("debug",s);
    }

    @Override
    public void userAdded(User user) {
        fragmentManager.beginTransaction().hide(signupFragment).commit();
        Log.d("debug","new user : "+user.getName());
    }

    @Override
    public void cancelSignin() {
        fragmentManager.beginTransaction().hide(signupFragment).commit();
        Log.d("debug","cancel signin");
    }
}
